package com.jhgsys.internal.accident.controller;

import com.jhgsys.internal.accident.entity.AccidentAuditRecord;
import com.jhgsys.internal.accident.service.IAccidentAuditRecordService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 事故上报审核记录 Controller
 *
 * @author admin
 * @date 2024-08-07 16:24:25
 */
@Slf4j
@Validated
@Controller
public class AccidentAuditRecordController extends BaseController {

    @Autowired
    private IAccidentAuditRecordService accidentAuditRecordService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAuditRecord")
    @RequiresPermissions("accidentAuditRecord:view")
    public String accidentAuditRecordIndex(){
        return FebsUtil.view("accidentAuditRecord/accidentAuditRecordList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAuditRecord/add")
    @RequiresPermissions("accidentAuditRecord:add")
    public String accidentAuditRecordAdd(){
        return FebsUtil.view("accidentAuditRecord/accidentAuditRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "accidentAuditRecord/update/{id}")
    @RequiresPermissions("accidentAuditRecord:edit")
    public String accidentAuditRecordUpdate(Model model, @PathVariable("id") Integer id){
        model.addAttribute("accidentAuditRecord", accidentAuditRecordService.findAccidentAuditRecordById(id));
        return FebsUtil.view("accidentAuditRecord/accidentAuditRecordEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @GetMapping("accidentAuditRecord")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:view")
    public FebsResponse getAllAccidentAuditRecords(AccidentAuditRecord accidentAuditRecord) {
        return new FebsResponse().success().data(accidentAuditRecordService.findAccidentAuditRecords(accidentAuditRecord));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @GetMapping("accidentAuditRecord/list")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:view")
    public FebsResponse accidentAuditRecordList(QueryRequest request, AccidentAuditRecord accidentAuditRecord) {
        Map<String, Object> dataTable = getDataTable(this.accidentAuditRecordService.findAccidentAuditRecords(request, accidentAuditRecord));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 AccidentAuditRecord
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @Log("新增AccidentAuditRecord")
    @PostMapping("accidentAuditRecord")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:add")
    public FebsResponse addAccidentAuditRecord(@Valid AccidentAuditRecord accidentAuditRecord) throws FebsException {
        try {
            accidentAuditRecord.setCreatedBy(getCurrentUser().getUserName());
            this.accidentAuditRecordService.createAccidentAuditRecord(accidentAuditRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增AccidentAuditRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 AccidentAuditRecord
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @Log("逻辑删除AccidentAuditRecord")
    @GetMapping("accidentAuditRecord/delete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:delete")
    public FebsResponse deleteAccidentAuditRecord(@PathVariable("id") String accidentAuditRecordIds) throws FebsException {
        try {
            AccidentAuditRecord accidentAuditRecord = new AccidentAuditRecord();
            accidentAuditRecord.setDeletedBy(getCurrentUser().getUserName());
            accidentAuditRecord.setDeletedTime(new Date());
            accidentAuditRecord.setDeletedFlg(FebsConstant.INVALID);
            this.accidentAuditRecordService.deleteAccidentAuditRecord(accidentAuditRecord,accidentAuditRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAuditRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 AccidentAuditRecord
    * @author admin
    * @date 2024-08-07 16:24:25
    */
    @Log("物理删除AccidentAuditRecord")
    @GetMapping("accidentAuditRecord/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:delete")
    public FebsResponse physicallyDeleteAccidentAuditRecord(@PathVariable("id") String accidentAuditRecordIds) throws FebsException {
        try {
            this.accidentAuditRecordService.physicallyDeleteAccidentAuditRecord(accidentAuditRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除AccidentAuditRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 AccidentAuditRecord
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @Log("修改AccidentAuditRecord")
    @PostMapping("accidentAuditRecord/update")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:edit")
    public FebsResponse updateAccidentAuditRecord(AccidentAuditRecord accidentAuditRecord) throws FebsException {
        try {
            accidentAuditRecord.setUpdatedBy(getCurrentUser().getUserName());
            this.accidentAuditRecordService.updateAccidentAuditRecord(accidentAuditRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改AccidentAuditRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 AccidentAuditRecord的excel
     * @author admin
     * @date 2024-08-07 16:24:25
     */
    @PostMapping("accidentAuditRecord/excel")
    @ResponseBody
    @RequiresPermissions("accidentAuditRecord:view")
    public void export(QueryRequest queryRequest, AccidentAuditRecord accidentAuditRecord, HttpServletResponse response) throws FebsException {
        try {
            List<AccidentAuditRecord> accidentAuditRecords = this.accidentAuditRecordService.findAccidentAuditRecords(queryRequest, accidentAuditRecord).getRecords();
            ExcelKit.$Export(AccidentAuditRecord.class, response).downXlsx(accidentAuditRecords, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
